package cn.lingnan.controller;

import cn.lingnan.dao.GoodsDao;
import cn.lingnan.entity.goods.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;


@Controller
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsDao goodsdao;

    /**
     * 查找全部
     * @param model
     * @return
     */
    @GetMapping(value="/findAllGoods")
    public String findAllGoods(Model model){
        List<Goods> goodsList = goodsdao.findAllGoods();
        model.addAttribute("goodsList",goodsList);
        return "goods-list";
    }
//    @GetMapping(value="/test1")
//    public void saveTest() throws Exception {
//        MongoTest mgtest=new MongoTest();
//        mgtest.setGoods_id("36");
//        mtdao.saveTest(mgtest);
//    }
    @GetMapping("editGoods")
    public String updateGoods(Model model, int goods_id){
        System.out.println("goods_id is :::"+goods_id);
        Goods good = goodsdao.findGoodsById(goods_id);
        model.addAttribute("good",good);
        return "good-edit";
    }

    @GetMapping("addGoods")
    public String addGoods(){
        return "good-add";
    }

    @PostMapping("insertGoods")
    @ResponseBody
    public String insertGoods(Goods good){
        goodsdao.insertGoods(good);
        return "success";
    }
    /**
     * 更新单个商品信息
     * @param good
     * @return
     */
    @PostMapping("updateGoods")
    @ResponseBody
    public String updateGoods(Goods good){
        goodsdao.updateGoods(good);
        return "success";
    }
    /**
     * 根据商品id查找单个商品
     * @return
     */
    @GetMapping(value="/findGoodsById")
    public Goods findGoodsById(int goods_id){
        Goods goods= goodsdao.findGoodsById(goods_id);
        return goods;
    }

    /**
     * 根据商品id删除单个商品
     * @param goods_id
     * @return
     */
    @PostMapping("deleteOneGoods")
    @ResponseBody
    public String deleteOneGoods(Integer goods_id){
        System.out.println("delete id is "+goods_id);
        goodsdao.deleteOneGoods(goods_id);
        return "success";
    }
}
